package com.wsoft.mq.test.config.topic;

import com.wsoft.mq.test.constant.RabbitTopicConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 主题交换机
 * @Author z001
 * @Date 2025/1/8 07:53
 */
@Configuration
public class TopicExchangeConfig {

    /**
     * 定义队列
     * @return
     */
    @Bean
    public Queue topicQueue1() {
        return new Queue(RabbitTopicConstant.TOPIC_QUEUE_1, true);
    }
    @Bean
    public Queue topicQueue2() {
        return new Queue(RabbitTopicConstant.TOPIC_QUEUE_2, true);
    }

    /**
     * 定义主题交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        // 持久化，不自动删除
        return new TopicExchange(RabbitTopicConstant.TOPIC_EXCHANGE, true, false);
    }


    /**
     * 定义绑定关系
     * @param topicQueue1
     * @param topicExchange
     * @return
     */
    @Bean
    public Binding topicBinding1(Queue topicQueue1, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue1).to(topicExchange).with("*.orange.*");
    }

    @Bean
    public Binding topicBinding2(Queue topicQueue2, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with("*.*.rabbit");
    }

    @Bean
    public Binding topicBinding3(Queue topicQueue2, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with("lazy.#");
    }

}
